package com.itheima.service.impl;

import com.itheima.constant.MessageConstant;
import com.itheima.mapper.*;
import com.itheima.pojo.Result;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import com.mysql.jdbc.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.StringWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberAgeMapper memberAgeMapper;

    @Autowired
    private MemberSexMapper memberSexMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Result getMemberReport() {
        //创建一个map集合,存放查询到的月份(x轴),和每个月份(y轴)的相对应的数量
        Map<String, Object> map = new HashMap<String, Object>();

        //创建集合存放月份
        List<String> months = new ArrayList<>();
        //创建集合存放每个月相对应的会员人数
        List<Integer> memberCounts = new ArrayList<>();

        //获取日历对象
        Calendar calendar = Calendar.getInstance();
        //设置日历往前推12个月
        calendar.add(Calendar.MONTH, -12);
        //遍历过去12个月的每一个月
        for (int i = 0; i < 12; i++) {
            //获取每个月的时间
            Date time = calendar.getTime();
            System.out.println(time+"---time---");
            //获取每个月的月份
            String date = new SimpleDateFormat("yyyy-MM").format(time);
            System.out.println(date+"----date---");
            //定义每个月开始日期
            String beginDate = date + "-1";
            //定义每个月结束时间
            String endDate = date + "-31";
            //统计每个月人数
            int count = memberMapper.findMemberCountByMonth(beginDate, endDate);
            //添加每一个月
            months.add(date);
            //添加每一个月的会员数量
            memberCounts.add(count);
            //每次循环都在日历的月份上+1，如-11月,-10月，-9月，依次类推
            calendar.add(Calendar.MONTH, +1);
        }
        //将moths与memberCounts分别存入到map集合中
        map.put("months", months);
        map.put("memberCounts", memberCounts);

        if (map == null) {
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        } else {
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL, map);
        }


    }

    @Override
    public Result findReportBusiness() {
        try {
            //创建一个map集合,以key:value的形式存储前端需要的数据
            Map map = new HashMap<String, Object>();

            //获取当前日期
            /*Date now = DateUtils.getToday();
            String today = DateUtils.parseDate2String(now);*/
            //获取当前日期(字符串类型)
            String today = DateUtils.getToday();

            //System.out.println("*******当前日期为:"+today);

            //存入今天的日期
            map.put("reportDate", today);

            //今日新增会员数
            int todayNewMember = memberMapper.getMemberCountsByDay(today);
            //存到map集合中
            map.put("todayNewMember", todayNewMember);

            //总会员数
            int totalMember = memberMapper.getMemberTotalCounts();
            map.put("totalMember", totalMember);

            //本周新增会员数,从星期一截止当前日期
            //获取当前周的星期一
            Date weekMondayeekMonday = DateUtils.getThisWeekMonday(new Date());
            String thisWeekMonday = DateUtils.parseDate2String(weekMondayeekMonday);
            //System.out.println("*******当前周的星期一的日期是:"+thisWeekMonday);
            int thisWeekNewMember = memberMapper.getMemberCountsByWeek(thisWeekMonday, today);
            map.put("thisWeekNewMember", thisWeekNewMember);

            //本月增会员数,从本月1号截止当前日期
            //获取本月1号
            Date firstDayOfMonth = DateUtils.getFirstDay4CurrentMonth();
            String firstDay = DateUtils.parseDate2String(firstDayOfMonth);
            //System.out.println("*******当前月的1号具体为:"+firstDay);
            int thisMonthNewMember = memberMapper.getMemberCountsByMoth(firstDay, today);
            map.put("thisMonthNewMember", thisMonthNewMember);

            //今日预约数
            int todayOrderNumber = orderMapper.getOrderCountsByDay(today);
            //存到map集合中
            map.put("todayOrderNumber", todayOrderNumber);

            //今日到诊数
            int todayVisitsNumber = orderMapper.getOrderCountsByVisitsAndDay(today);
            map.put("todayVisitsNumber", todayVisitsNumber);

            //本周预约数,从星期一截止当前日期
            int thisWeekOrderNumber = orderMapper.getOrderCountsByWeek(thisWeekMonday, today);
            map.put("thisWeekOrderNumber", thisWeekOrderNumber);

            //本周到诊数,从星期一截止当前日期
            int thisWeekVisitsNumber = orderMapper.getOrderCountsByWeekAndVisits(thisWeekMonday, today);
            map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);

            //本月预约数,从本月1号截止当前日期
            int thisMonthOrderNumber = orderMapper.getOrderCountsByMoth(firstDay, today);
            map.put("thisMonthOrderNumber", thisMonthOrderNumber);

            //本月到诊数,从本月1号截止当前日期
            int thisMonthVisitsNumber = orderMapper.getOrderCountsByMothAndVisits(firstDay, today);
            map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);

            //热门套餐
            //创建一个list集合,存储人们套餐
            //List<Map<String,Object>> hostSetmeals = new ArrayList<Map<String,Object>>();
            List<Map<String, Object>> hotSetmeals = orderMapper.rankOrderBySetMealCounts();
            map.put("hotSetmeals", hotSetmeals);
            //System.out.println("热门套餐有:"+hotSetmeals);

            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @Override
    public Result getSetmealReport() {
        try {
            //创建一个大集合,存储前端需要的数据
            Map map = new HashMap<String, Object>();
            //创建一个集合存储name
            List<String> names = new ArrayList<>();
            //创建一个集合以key:value的形式存储饼状图需要的详细数据
            List<Map> listMap = setMealMapper.getSetmealReport();
            ///遍历循环
            for (Map map1 : listMap) {
                String setmealName = (String) map1.get("name");
                names.add(setmealName);
            }
            map.put("setmealNames", names);
            map.put("setmealCounts", listMap);
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @Override
    public Result getMemberSexReport() {
        try {
            //创建一个大集合,存储前端需要的数据
            Map map = new HashMap<String,Object>();
            //创建一个集合存储name
            List<String> names = new ArrayList<>();
            //创建一个集合以key:value的形式存储饼状图需要的详细数据
            List<Map>  listMap = memberSexMapper.getMemberSexReport();
            ///遍历循环
            for (Map map1 : listMap) {
                String  setmealName = (String)map1.get("name");
                names.add(setmealName);
            }
            map.put("memberSexIs",names);
            map.put("memberSexCounts",listMap);
            System.out.println("========="+map);
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @Override
    public Result getMemberAgeReport() {
        try {
            //创建一个大集合,存储前端需要的数据
            Map map = new HashMap<String, Object>();
            //创建一个集合存储name
            List<String> names = new ArrayList<>();
            //创建一个集合以key:value的形式存储饼状图需要的详细数据
            List<Map> listMap = memberAgeMapper.getMemberAgeReport();

            List<Map<String, Object>> hashMapList = new ArrayList<>();

            ///遍历循环
            for (Map map1 : listMap) {
                //循环后 map{value:4,年龄段}
                //创建新的map集合
                Map<String, Object> hm = new HashMap<>();
                hm.put("value", map1.get("数量"));
                hm.put("name", map1.get("年龄段"));
                hashMapList.add(hm);

                String setmealName = (String) map1.get("年龄段");
                names.add(setmealName);
            }


            map.put("memberAgeIs", names);
            map.put("memberAgeCounts", hashMapList);
            System.out.println("=========" + map);
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @Override
    public Result findMemberByDate(String startDate, String endDate) throws ParseException {
        //创建一个map集合,存放查询到的月份(x轴),和每个月份(y轴)的相对应的数量
        Map<String, Object> map = new HashMap<String, Object>();
        Date sdate = DateUtils.parseString2Date(startDate,"yyyy-MM-dd");
        Date edate = DateUtils.parseString2Date(endDate,"yyyy-MM-dd");
        System.out.println(sdate+"=前台数据===="+edate);

        System.out.println();

        //创建集合存放月份
        List<String> months = new ArrayList<>();
        //创建集合存放每个月相对应的会员人数
        List<Integer> memberCounts = new ArrayList<>();


        int eyear = Integer.parseInt(endDate.substring(0, 4));
        System.out.println(eyear+"年");
        int month1 = edate.getMonth();
        System.out.println(month1+"月");

        int syear = Integer.parseInt(startDate.substring(0, 4));
        System.out.println(syear+"年");
        int month2 = sdate.getMonth();
        System.out.println(month2+"月");
        //
        if (eyear != syear) {
            //年份不一样，则先循环前一年所有会员信息
            //将初始日期时间赋予变量date1
            int month = sdate.getMonth();
            for (int j = month; j < 12; j++) {
                //定义每个月开始日期
                String beginDate = syear + "-" + (j+1) + "-1";
                //定义每个月结束时间
                String endDatee = syear + "-" + (j+1) + "-31";
                //统计每个月会员数量
                int count = memberMapper.findMemberCountByMonth(beginDate, endDatee);
                //向集合添加年月份
                String monthh = syear +"-"+ (j+1);
                months.add(monthh);
                memberCounts.add(count);
            }
            System.out.println("------2019年-----");
            for (String s : months) {
                System.out.println(s);
            }
            for (int j = 0; j <= edate.getMonth(); j++) {
                //定义每个月开始日期
                String beginDate = eyear + "-" + (j+1) + "-1";
                //定义每个月结束时间
                String endDatee = eyear + "-" + (j+1) + "-31";
                //统计每个月会员数量
                int count = memberMapper.findMemberCountByMonth(beginDate, endDatee);
                //向集合添加年月份
                String monthh = eyear + "-"+(j+1);
                months.add(monthh);
                memberCounts.add(count);
            }
            System.out.println("------2020年-----");
            for (String s : months) {
                System.out.println(s);
            }
        } else {
            //年份相同
            int month = sdate.getMonth();
            for (int i = month; i <= edate.getMonth(); i++) {
                //定义每个月开始日期
                String beginDate = eyear+"-"+(i+1)+"-1";
                //定义每个月结束时间
                String endDatee = eyear+"-"+(i+1)+"-31";
                //统计每个月会员数量
                int count = memberMapper.findMemberCountByMonth(beginDate, endDatee);
                //向集合添加年月份
                String monthh = eyear+"-" + (i+1);

                months.add(monthh);
                memberCounts.add(count);
            }
        }
        //将moths与memberCounts分别存入到map集合中
        map.put("months", months);
        map.put("memberCounts", memberCounts);

        if (map == null) {
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        } else {
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL, map);
        }

    }
}
